package com.hzk.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 * @auoter:jackh
 */
@Component
public class MD5Util {
    private static final String salt = "1a2b3c4d";

    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    /*前后端的salt需要统一*/
    /*这一段代码就是加密前端到后端的代码*/
    public static String inputPassToFromPass(String inputPass) {
        String str = ""+salt.charAt(0) + salt.charAt(2) + inputPass +
                salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /*后端传到数据库的salt就是随机的*/
    public static String formPassToDBPass(String fromPass, String salt) {
        String str = ""+salt.charAt(0) + salt.charAt(2) + fromPass +
                salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }/*d3b1294a61a07da9b49b6e22b2cbd7f9
    99d3b1294a61a07da9b49b6e22b2cbd7f9c3*/



    public static String inputPassToDBPass(String inputPass, String salt) {
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDBPass(fromPass, salt);
        return dbPass;
    }

    public static void main(String[] args) {
        String a="123456";
        String x = inputPassToFromPass("123456");
        System.out.println(x);
        /*第一次加密*/
        System.out.println(formPassToDBPass(a,"1a2b3c"));
        System.out.println(inputPassToDBPass("123456", "1a2b3c4d"));
        System.out.println();
    }
   /* d3b1294a61a07da9b49b6e22b2cbd7f9
    0687f9701bca74827fcefcd7e743d179*/

     /*d3b1294a61a07da9b49b6e22b2cbd7f9
       d3b1294a61a07da9b49b6e22b2cbd7f9
            b7797cce01b4b131b433b6acf4add449*/
    /*d3b1294a61a07da9b49b6e22b2cbd7f9*/

}
